var express = require('express'); //引入框架
var db = require('./db2.js')
var route = express.Router();
var bodyParser = require('body-parser');
var urlencoded = bodyParser.urlencoded({
  extended: false
})

route.get('/list', async (req, res)=> {
  var sql = 'select * from roles'
  let {resultes} = await db.doSql(sql)
  res.send(db.hand(resultes));
})

route.post('/adddata', urlencoded, async (req, res) => {
  let {
    title,
    role
  } = req.body
  let sql = `insert into roles (title, role) values ('${title}', '${role}')`
  let data = await db.doSql(sql);
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Content-Type", "text/html; charset=utf-8")
  res.send(JSON.stringify(db.hand(data)))
})

route.post('/upData', urlencoded, async (req, res) => {
  let {
    items,
    id
  } = req.body
  console.log(req.body)
  let sql = `update roles set items='${items}' where id=${id}`;
  let data = await db.doSql(sql)
  res.send(JSON.stringify(db.hand(data.affectedRows)));
})

route.get('/remove', async (req, res) => {
  let sql = `delete from roles where id in (${req.query.id})`;
  let data = await db.doSql(sql);
  res.send(db.hand(data.affectedRows));
})

route.get('/validation', async (req, res) => {
  let sql = `select * from roles where role like '${req.query.roles}'`;
  let data = await db.doSql(sql);
  console.log(data.resultes[0])
  res.send(db.hand(data.resultes[0]));
})

module.exports = route
